﻿<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <#include "../common/header.ftl"/>
    <link rel="stylesheet" href="/js/plugins/treeview/bootstrap-treeview.min.css" type="text/css" />
    <script type="text/javascript" src="/js/plugins/treeview/bootstrap-treeview.min.js"></script>

    <script>
        $(function () {
            $.get('/region/selectByParentId.do',{type:'tree'},function (data) {
                var defaultData = [{text: '全部地区', nodes: data}];
                $('#treeview1').treeview({
                    data: defaultData,//要显示的数据(必须规定的格式)
                    showTags: true, //开启标签的显示
                    lazyLoad:function (node) {//懒加载(点击+号时会触发),参数是点击的那个地区
                        //根据上级地区查询子地区
                        $.get('/region/selectByParentId.do',{parentId:node.id,type:'tree'},function (data) {
                            console.log(data)//第一个参数是子地区的数据,第二个是上级地区
                            $('#treeview1').treeview('addNode', [ data, node]);
                        })
                    },
                    onNodeSelected: function(event, node) { //节点选中事件
                        $.get('/region/selectByParentId.do',{parentId:node.id},function (data) {
                            $(".table tbody").empty();//清空tbody的内容
                            console.log("data: ",data)
                            $.each(data,function(index,ele){
                                console.log(ele)
                                //拷贝模板
                                var tr = $("#template tr").clone(true);
                                //填充数据
                                $(tr).find('td:nth-child(1)').html(index+1);
                                $(tr).find('td:nth-child(2)').html(ele.name);
                                $(tr).find("a").attr('data-json',ele.json); //设置自定义属性
                                //根据地区的状态来设置按钮文字
                                if(ele.state==1){
                                    $(tr).find("a.btn-hot").html("<span class=\"glyphicon glyphicon-remove\"></span> 取消推荐")
                                }

                                //添加到要显示的table里面
                                $(".table tbody").append(tr);
                            })
                        })
                    }
                });
            })


            //点击添加按钮,显示模态框
            $(".btn-input").click(function () {
                //每次先将表单中的数据清空
                $("#editForm")[0].reset();

                //获取按钮上的data-json的数据
                var json = $(this).data("json");
                if(json){
                    console.log(json);
                    //将获取到的数据设置到表单对应的元素中
                    $("input[name=id]").val(json.id);
                    $("input[name=name]").val(json.name);
                    $("#parentName").val(json.parentName);
                    $("#parentId").val(json.parentId);
                }else{
                    //获取选中的地区节点,设置到模态框中
                    var arr = $('#treeview1').treeview('getSelected');
                    if(arr!=null&&arr.length>0) {
                        $("#parentName").val(arr[0].text);
                        $("#parentId").val(arr[0].id);
                    }
                }
                $("#editModal").modal("show");
            })

            //提交表单
            $(".btn-submit").click(function () {
                $("#editForm").ajaxSubmit(function (data) {
                    handleResult(data);
                })
            })
            
            //设为推荐
            $(".btn-hot").click(function () {
                var json = $(this).data('json');
                $.post('/region/updateState.do',{id:json.id,state:json.state^1},function(data){
                    handleResult(data);
                })
            })

        })
    </script>




</head>
<body>

<!--table的模板-->
<table id="template" style="display: none">
<tr>
    <td></td>
    <td></td>
    <td>
        <a class="btn btn-info btn-xs btn-input" href="javascript:;"  >
            <span class="glyphicon glyphicon-pencil"></span> 编辑
        </a>
        <a href="javascript:;" class="btn btn-danger btn-xs btn-hot"  >
            <span class="glyphicon glyphicon-trash"></span> 设为推荐
        </a>
    </td>
</tr>
</table>

<div class="container " style="margin-top: 20px">
    <#include "../common/top.ftl"/>
    <div class="row">
        <div class="col-sm-3">
            <#--设置当前要回显当前菜单,必须在载人菜单前完成设置-->
            <#assign menu="region"/>
            <#include "../common/menu.ftl">
        </div>
        <div class="col-sm-9">
            <div class="row">
                <div class="col-sm-12">
                    <h1 class="page-head-line">旅游地区管理</h1>
                </div>
            </div>
            <!--高级查询--->
            <form class="form-inline" id="searchForm" action="" method="post">
                <input type="hidden" id="currentPage" name="currentPage" value="1">
                <a href="javascript:;" class="btn btn-success btn-input">
                    <span class="glyphicon glyphicon-plus"></span> 添加
                </a>
            </form>

            <div class="row">
                <div class="col-sm-4">
                    <div id="treeview1"></div>
                </div>
                <div class="col-sm-8">
                  <table class="table table-striped table-hover" >
                      <thead>
                        <tr>
                            <th>编号</th>
                            <th>名称</th>
                            <th>操作</th>
                        </tr>
                      </thead>
                      <tbody></tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

<!--编辑模态框-->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title">编辑</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" action="/region/saveOrUpdate.do" method="post" id="editForm">
                    <input type="hidden" name="id">
                    <div class="form-group">
                        <label for="name" class="col-sm-4 control-label">名称：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="name" name="name"
                                   placeholder="请输入地区名称">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="sn" class="col-sm-4 control-label">上级地区：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="parentName" readonly>
                            <input type="hidden" class="form-control" id="parentId" name="parent.id" >
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary btn-submit">保存</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->




</body>
</html>